package com.shopmall.controller;

import com.shopmall.pojo.Admin;
import com.shopmall.pojo.AdminRecord;
import com.shopmall.pojo.BuyList;
import com.shopmall.pojo.Goods;
import com.shopmall.service.AdminRecordServiceImpl;
import com.shopmall.service.BuyListServiceImpl;
import com.shopmall.service.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
public class BuyListController {

    @Autowired
    private BuyListServiceImpl buyListService;

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private AdminRecordServiceImpl adminRecordService;

    @RequestMapping("/buy_list/buy_list_show")
    public String buy_list_show(Model model){
        List<BuyList> buy_list_list=buyListService.queryBuyListList();
        model.addAttribute("buy_list_list",buy_list_list);
        return "buy_list/buy_list_show";
    }

    @RequestMapping("/buy_list/buy_list_show_end")
    public String buy_list_show_end(Model model){
        List<BuyList> buy_list_list=buyListService.queryBuyListListEnd();
        model.addAttribute("buy_list_list",buy_list_list);
        return "buy_list/buy_list_show_end";
    }

    @RequestMapping("/buy_list/modify_buy_list/{id}")
    public String modify_buy_list(@PathVariable("id") int id,Model model){
        BuyList buy_list = buyListService.queryBuyListById(id);
        List<Goods> goodsList=goodsService.queryGoodsList();

        model.addAttribute("buy_list",buy_list);
        model.addAttribute("goodsList",goodsList);
        return "buy_list/modify_buy_list";
    }

    @RequestMapping("/buy_list/modify_buy_list_do")
    public String modify_buy_list_do(BuyList buyList, Model model, HttpSession session){
        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who(""+buyList.getBuy_list_id());
        record.setDone_role("商品订单");
        record.setDo_what("修改商品订单");
        //以上是记录信息设置


        if(buyList.getState()==3){
            Timestamp arrive_time = new Timestamp(System.currentTimeMillis());
            buyList.setArrive_time(arrive_time);
        }
        buyListService.updateBuyList(buyList);
        adminRecordService.addRecord(record);
        model.addAttribute("msg","成功修改编号为 "+buyList.getBuy_list_id()+" 的商品订单");
        return "redirect:/buy_list/buy_list_show";
    }

    //以上是真正使用的函数

    /*
    @GetMapping("/queryBuyListList")
    List<BuyList> queryBuyListList(){
        return buyListService.queryBuyListList();
    }

    @GetMapping("/queryBuyListByUsername/{username}")
    List<BuyList> queryBuyListByUsername(@PathVariable("username") String username){
        return buyListService.queryBuyListByUsername(username);
    }

    @GetMapping("/queryBuyListByGoods/{goods_name}")
    List<BuyList> queryBuyListByGoods(@PathVariable("goods_name") String goods_name){
        return buyListService.queryBuyListByGoods(goods_name);
    }

    @GetMapping("/queryBuyListById/{id}")
    BuyList queryBuyListById(@PathVariable("id") int id){
        return buyListService.queryBuyListById(id);
    }

    @PostMapping("/updateBuyList")
    int updateBuyList(BuyList buyList){
        return buyListService.updateBuyList(buyList);
    }

    @GetMapping("/deleteBuyList/{id}")
    int deleteBuyList(@PathVariable("id") int id){
        return buyListService.deleteBuyList(id);
    }
    */

}
